package com.framework.core.text;

import java.nio.charset.Charset;
import java.util.Base64;

/**
 * Base64工具类
 * Created by c14059 on 2017/8/3.
 */
public class Base64Util {

    private static final Base64.Decoder DECODER = Base64.getDecoder();

    private static final Base64.Encoder ENCODER = Base64.getEncoder();

    private static final Charset ENCODING = Charset.forName("UTF-8");

    /**
     * Base64 编码
     *
     * @param str
     * @return
     */
    public static String encodeToString(String str) {
        if (str == null) {
            return null;
        }
        return ENCODER.encodeToString(str.getBytes(ENCODING));
    }

    /**
     * Base64 解码
     *
     * @param str
     * @return
     */
    public static String decodeToString(String str) {
        if (str == null) {
            return null;
        }
        return new String(DECODER.decode(str), ENCODING);
    }
}
